/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package engine.entities.inventory.item;

import engine.entities.Player;
import engine.entities.inventory.Inventory;
import static engine.entities.inventory.item.InventoryTable.ITEM_RENDER_SCALE;
import engine.entities.inventory.item.MobEquipSet.EQUIP_SLOT;
import engine.resource.Resources;

/**
 *
 * @author NathanT
 */
public class HeadTable extends InventoryTable {
    
    private Player player;
    private static final EQUIP_SLOT SLOT = EQUIP_SLOT.HEAD;
    private static final String ICON_NAME = "HEAD_SLOT_ICON";
    
    public HeadTable(Player player) {
        super(1, 1);
        this.setInventoryTableListener(new HeadTableListener());
        this.player = player;
    }

    @Override
    protected void initRenderers() {
        this.items[0][0] = new InventoryItemRenderer(null, Resources.getSprite(ICON_NAME), SLOT);
    }
    
    public final void setPlayer(Player player) {
        this.player = player;
        if (player == null) {
            emptyTable();
            return;
        }
        
        MobEquipSet set = player.getEquipSet();
        Item item;
        
        item = set.getItem(SLOT);
        if (item != null) {
            this.items[0][0].setInvItem(new InventoryItem(item, 1));
        }
        else {
            this.items[0][0].setInvItem(null);
        }
    }
    
    private void emptyTable() {
        this.items[0][0].setInvItem(null);
    }
    
    private class HeadTableListener implements InventoryTableListener {

        @Override
        public void itemChanged(int row, int column, InventoryItem newItem) {
            if (player != null) {
                if (newItem != null) {
                    player.getEquipSet().setItem(SLOT, newItem.getItem());
                }
                else {
                    player.getEquipSet().setItem(SLOT, null);
                }
            }
        }
        
    }
    
}
